package com.weixin.tools;

import com.weixin.bo.menu.Button;
import com.weixin.bo.menu.CommonButton;
import com.weixin.bo.menu.ComplexButton;
import com.weixin.bo.menu.KeyButton;
import com.weixin.bo.menu.Menus;
import com.weixin.bo.menu.ViewButton;
import com.weixin.sdk.api.AccessTokenApi;
import com.weixin.sdk.api.ApiResult;
import com.weixin.sdk.kit.HttpKit;
import com.weixin.sdk.kit.PropKit;

/**
 * menu api
 */
public class ToolMenu {
	
	private static String getMenu = "https://api.weixin.qq.com/cgi-bin/menu/get?access_token=";
	private static String createMenu = "https://api.weixin.qq.com/cgi-bin/menu/create?access_token=";
	private static String deleteMenu = "https://api.weixin.qq.com/cgi-bin/menu/delete?access_token=";

	/**
	 * 查询菜单
	 */
	public static ApiResult getMenu() {
		String jsonResult = HttpKit.get(getMenu + AccessTokenApi.getAccessToken().getAccessToken());
		return new ApiResult(jsonResult);
	}
	
	/**
	 * 创建菜单
	 */
	public static ApiResult createMenu(String jsonStr) {
		String jsonResult = HttpKit.post(createMenu + AccessTokenApi.getAccessToken().getAccessToken(), jsonStr);
		return new ApiResult(jsonResult);
	}
	
	/**
	 * 删除菜单
	 */
	public ApiResult deleteMenu() {
		String jsonResult = HttpKit.get(deleteMenu + AccessTokenApi.getAccessToken().getAccessToken());
		return new ApiResult(jsonResult);
	}
	
	/**
	 * 主菜单
	 * 
	 * @return
	 */
	public static String getMainMenu() {
		StringBuffer buffer = new StringBuffer();
		buffer.append("您好，我是话唠，请回复数字选择服务：").append("\n\n");
		buffer.append("回复 0 查看菜单;").append("\n");
		buffer.append("回复 1 查看历史上的今天;").append("\n");
		buffer.append("回复 2 本周推荐;").append("\n");
		buffer.append("回复 3 查看星座运势;").append("\n");
		buffer.append("回复 4 轻松一刻;").append("\n");
		buffer.append("回复 5 快递查询;").append("\n");
		
		buffer.append("回复“?”显示此帮助菜单");
		return buffer.toString();
	}
	
	/**
	 * 组装菜单数据
	 * @return
	 */
	public static Menus getMenuButton(String weixinId,String appid) {

//		var url=_host+'/upingan/upingan/activity/mobileCard/getActivity.html?name='+name+'&enkeyValue='+mobile;
//		var url="http%3A%2F%2F"+location.host+'/upingan/upingan/activity/mobileCard/getActivity.html%253Fname%253D'+encodeURIComponent(name)+'%2526enkeyValue%253D'+encodeURIComponent(mobile);
//		var _url='https://open.weixin.qq.com/connect/oauth2/authorize?appid=wxf491dc164185d94c&redirect_uri=http%3A%2F%2Feim-talk-stg.dmzstg.pingan.com.cn%2Fpacx-pir%2FsimpleRedirect%3FbackUrl%3D'+url+'&response_type=code&scope=snsapi_base&state=simple#wechat_redirect'

		String hostUrl=PropKit.get("hostUrl");
		String snsapi_base_url=ToolString.urlEncode(hostUrl+"/api/oauth2?weixinId="+weixinId+"&backUrl=/message/index.html");		
				
		String snsapi_userinfo_url=ToolString.urlEncode(hostUrl+"/api/oauth2?weixinId="+weixinId+"&backUrl=/index.html");		

		
		String snsapi_base="https://open.weixin.qq.com/connect/oauth2/authorize?"
				+ "appid="+appid+"&redirect_uri="+snsapi_base_url
				+ "&response_type=code&scope=snsapi_base&state=simple#wechat_redirect";
		
		String snsapi_userinfo="https://open.weixin.qq.com/connect/oauth2/authorize?"
				+ "appid="+appid+"&redirect_uri="+snsapi_userinfo_url
				+ "&response_type=code&scope=snsapi_userinfo&state=simple#wechat_redirect";
		
		CommonButton btn11 = new CommonButton();
		btn11.setName("文本消息");
		btn11.setType("click");
		btn11.setKey("11");

		CommonButton btn12 = new CommonButton();
		btn12.setName("图片消息");
		btn12.setType("click");
		btn12.setKey("12");

		CommonButton btn13 = new CommonButton();
		btn13.setName("地理位置消息");
		btn13.setType("location_select");
		btn13.setKey("13");

		CommonButton btn14 = new CommonButton();
		btn14.setName("链接消息");
		btn14.setType("click");
		btn14.setKey("14");
		
		CommonButton btn15 = new CommonButton();
		btn15.setName("事件推送");
		btn15.setType("click");
		btn15.setKey("15");
		
		KeyButton key11=new KeyButton();
		key11.setType("scancode_waitmsg");
		key11.setName("扫码带提示");
		key11.setKey("rselfmenu_0_0");
		key11.setSub_button(new Button[]{});
		
		KeyButton key12=new KeyButton();
		key12.setType("scancode_push");
		key12.setName("扫码推事件");
		key12.setKey("rselfmenu_0_1");
		key12.setSub_button(new Button[]{});
    	
		KeyButton key13=new KeyButton();
		key13.setType("pic_sysphoto");
		key13.setName("系统拍照发图");
		key13.setKey("rselfmenu_0_3");
		key13.setSub_button(new Button[]{});
		
		KeyButton key14=new KeyButton();
		key14.setType("pic_photo_or_album");
		key14.setName("拍照或者相册发图");
		key14.setKey("rselfmenu_0_4");
		key14.setSub_button(new Button[]{});
		
		KeyButton key15=new KeyButton();
		key15.setType("pic_weixin");
		key15.setName("微信相册发图");
		key15.setKey("rselfmenu_0_5");
		key15.setSub_button(new Button[]{});
		
		
		ViewButton view11 = new ViewButton();
		view11.setName("留言板");
		view11.setType("view");
		view11.setUrl(snsapi_base);
		
		ViewButton view12 = new ViewButton();
		view12.setName("个人信息");
		view12.setType("view");
		view12.setUrl(snsapi_userinfo);
		
		ViewButton view13 = new ViewButton();
		view13.setName("后台管理系统");
		view13.setType("view");
		view13.setUrl(hostUrl);
		
		ComplexButton mainBtn1 = new ComplexButton();
		mainBtn1.setName("按钮事件");
		mainBtn1.setSub_button(new CommonButton[] { btn11, btn12, btn13, btn14, btn15});

		ComplexButton mainBtn2 = new ComplexButton();
		mainBtn2.setName("jsapi");
		mainBtn2.setSub_button(new KeyButton[] { key11, key12, key13, key14, key15});

		ComplexButton mainBtn3 = new ComplexButton();
		mainBtn3.setName("链接事件");
		mainBtn3.setSub_button(new Button[] { view11, view12, view13});

		/**
		 * 这是公众号xiaoqrobot目前的菜单结构，每个一级菜单都有二级菜单项<br>
		 * 
		 * 在某个一级菜单下没有二级菜单的情况，menu该如何定义呢？<br>
		 * 比如，第三个一级菜单项不是“更多体验”，而直接是“幽默笑话”，那么menu应该这样定义：<br>
		 * menu.setButton(new Button[] { mainBtn1, mainBtn2, btn33 });
		 */
		Menus menu = new Menus();
		menu.setButton(new Button[] { mainBtn1, mainBtn2, mainBtn3 });

		return menu;
	}
}



